package com.example.caimogu.businessapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.caimogu.http.HttpClientUtil;
import com.google.android.gms.search.SearchAuth;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;




public class LoginActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etPsd;
    private String account;
    private String psd;
    private String onlyBid;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

    }

    private void initView() {
        etName = (EditText) findViewById(R.id.et_login_username);
        etPsd = (EditText) findViewById(R.id.et_login_password);
    }
    public void isPermission(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClientUtil httpClientUtil = new HttpClientUtil();
                account = etName.getText().toString();
                psd = etPsd.getText().toString();
                HttpResponse response = httpClientUtil.businessLogIn(account, psd);
                Message message = new Message();
                HttpEntity entity = response.getEntity();
                if(response.getStatusLine().getStatusCode() == 200){
                    //如果输入正确，则返回一个String的ID,否则而返回错误网页，StatusCode为500
                    //很机智
                    Log.i("caimogu","500-----------------------------------------");
                    message.what = 1;
                    try {
                        onlyBid = EntityUtils.toString(entity);

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    handler.sendMessage(message);
                    return;
                }else{
                    message.what = 2;
                    handler.sendMessage(message);
                }


            }
        }).start();

    }
    //登录
    public void btnSignIn(View view){
        isPermission();

    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            if(msg.what == 1){

                Intent intent = new Intent();
                intent.putExtra("onlyBid",onlyBid);
                intent.setClass(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }else if(msg.what ==2) {
                Toast.makeText(LoginActivity.this, "用户名或者密码输入有误", Toast.LENGTH_SHORT).show();
            }

        }
    };


    //注册
    public void btnSignUp(View view){
        Intent intent = new Intent();
        intent.setClass(this,SignUpActivity.class);
        startActivityForResult(intent, 1);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == 2) {
                if(data.getStringExtra("signUpSuccess").equals("true")) {
                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                }

            }
        }


    }
}
